package com.demo.patch.validator;

import com.demo.patch.model.Patch;
import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;

/**
 * PatchValidator.
 */
public class PatchValidator extends Validator {

  protected void validate(Controller controller) {
    // 确保Para被正确初始化
    controller.getFile();
    validateRequiredString("patch.name", "titleMsg", "请输入补丁名称!");
    validateRequiredString("patch.username", "usernameMsg", "请输入补丁用户名!");
    validateRequiredString("patch.modulename", "modulenameMsg", "请输入补丁所属模块!");
    validateRequiredString("patch.version", "versionMsg", "请输入补丁适用版本!");
  }

  protected void handleError(Controller controller) {
    controller.keepModel(Patch.class);

    String actionKey = getActionKey();
    if (actionKey.equals("/patch/save"))
      controller.render("add.html");
    else if (actionKey.equals("/patch/update"))
      controller.render("edit.html");
  }
}
